/*
 *  This file is part of Social Media Monitoring Toolbox.
 *
 *  Social Media Monitoring Toolbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Social Media Monitoring Toolbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Social Media Monitoring Toolbox. If not, see <http://www.gnu.org/licenses/lgpl.html>.
 *
 * � Copyright 2013
 * Jana Asmussen, Julian Bau�, Daniela Dalock, Christian Gutjahr,
 * Fabian Heidorn, Alexander Kaack, Vitali Kagadij 
 * 
 */

package gui;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.LineChartSeries;

import types.FilterTyp;
import types.StreamDescriptor;
import types.TweetPost;
import types.YoutubeVideoType;

@ManagedBean(name = "chartCategoryBean")
public class ChartCategoryBean implements Serializable {

	private static final long serialVersionUID = -5691075244940944292L;
	private CartesianChartModel categoryModel;
	private GetData data = new GetData();
	private String count = "0";

	public ChartCategoryBean() {
		createCategoryModel();
	}

	/**
	 * Creates a new category model for the chart in the application. Sets the
	 * values and labels for the series for this chart
	 * 
	 */
	private void createCategoryModel() {
		categoryModel = new CartesianChartModel();

		// Setting the positive Sentiment
		setCategory("positive", 0.0, "sentiment");

		// Setting the negative Sentiment
		setCategory("negative", 0.0, "sentiment");

		// Setting the neutral Sentiment
		setCategory("neutral", 0.0, "sentiment");
	}

	/**
	 * Creates a new category model, that replaces the old one, with the values
	 * from the stream with id from parameter
	 * 
	 * @param ae
	 *            ActionEvent, that triggers this method
	 * @param id
	 *            represents the stream, which values should be shown
	 */
	public void updateChart(ActionEvent ae, String id) {
		categoryModel = null;
		categoryModel = new CartesianChartModel();

		List<StreamDescriptor> streams = data.getStreams();

		StreamDescriptor stream = data.getStreamDescriptor(id, streams);
		if (stream.getObjectId() != null) {
			// Getting the percentageSentiment for a special stream
			double[] values = data.getPercentageSentiment(stream);

			// Setting the positive Sentiment
			setCategory("positive", values[0], "sentiment");

			// Setting the negative Sentiment
			setCategory("negative", values[1], "sentiment");

			// Setting the neutral Sentiment
			setCategory("neutral", values[2], "sentiment");
		}
	}
	
	/**
	 * Count all posts for the selected stream
	 * 
	 * @param ae
	 *            ActionEvent, that triggers this method
	 * @param id
	 *            id of the stream
	 */
	public void updateCount(ActionEvent ae, String id){
		int count = 0;
		
		List<StreamDescriptor> streams = data.getStreams();
		StreamDescriptor stream = data.getStreamDescriptor(id, streams);
		
		if(stream.getElementType() == YoutubeVideoType.class){
			count = data.getYouTubeClips(stream, new FilterTyp()).size();
		}
		else if(stream.getElementType() == TweetPost.class){			
			count = data.getTweets(stream, new FilterTyp()).size();
		}
		
		this.count = Integer.toString(count);
	}
	
	/**
	 * Setting the category
	 * 
	 * @param label
	 *            value-label
	 * @param value
	 *            chart value
	 * @param topic
	 *            chart topic
	 */
	private void setCategory(String label, double value, String topic) {
		ChartSeries topicchart = new ChartSeries();
		topicchart.setLabel(label);
		topicchart.set(topic, value);
		categoryModel.addSeries(topicchart);
	}
	
	/**
	 * Returns the current category model of the chart in the application
	 * 
	 * @return the category model of the chart
	 */
	public CartesianChartModel getCategoryModel() {
		return categoryModel;
	}	

	/**
	 * Returns the sum of all posts
	 * 
	 * @return count of posts
	 */
	public String getCount() {
		return count;
	}

	
}